﻿/*
 * Processor.h
 *
 *  Created on: 2016年2月22日
 *      Author: Administrator
 */

#ifndef CORE_SOURCES_PROCESS_PROCESSOR_H_
#define CORE_SOURCES_PROCESS_PROCESSOR_H_
#include "conf/def.h"
#include <boost/noncopyable.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/bind.hpp>
#include "basic/singleton.hpp"
#include "thread/thread_pool/thread_pool.h"
#include "thread/task/sequential_task.h"
#include "thread/single/service.h"
#include "chan.hpp"
#include "timer.h"
namespace eyas{
namespace process{
	
typedef eyas::thread::sequential_task_ptr sequential_task_ptr;

class EYAS_PROCESS_API processor : public boost::noncopyable, public eyas::basic::singleton<processor> {
public:
	processor();
	virtual ~processor();

private:
	eyas::thread::thread_pool_ptr threadPoolPtr_;
public:
	// 创建顺序执行的任务序列（仅是顺序执行，不会绑定线程，各任务会被不同的线程顺序调用执行）
	sequential_task_ptr create_sequential_task();
	
	template<typename T>
	chan<T> create_chan();

	// 异步执行任务
	template<typename F, typename... Args>
	void schedule(F taskFunc, Args... args);
};

template<typename T>
chan<T> processor::create_chan(){
	return chan<T>(eyas::gc::create_new<chan_impl<T>>());
}
template<typename F, typename... Args>
inline void processor::schedule(F task_func, Args... args){
	this->threadPoolPtr_->schedule(task_func, args);
}


void init();
void clean();

template<typename T>
chan<T> create_chan(){
	return processor::instance()->create_chan<T>();
}

sequential_task_ptr create_sequential_task();
template<typename F, typename... Args>
void fly(sequential_task_ptr seq_task_ptr, F taskFunc, Args... args){
	seq_task_ptr->schedule(taskFunc, args...);
}

template<typename F, typename... Args>
void fly(F task_func, Args... args){
	processor::instance()->schedule(task_func, args...);
}

template<typename F, typename... Args>
timer_ptr fly_timer(int delay, int period, F timer_func, Args... args){
	timer_ptr timerPtr = eyas::gc::create_new<timer>(timer_func, args...);
	timerPtr->start(delay, period);
	return timerPtr;
}
template<typename F, typename... Args>
timer_ptr fly_timer(int delay, F timer_func, Args... args){
//	timer_ptr timerPtr(new timer());
//	timerPtr->schedule(boost::bind(timerFunc), delay);
	timer_ptr timerPtr = eyas::gc::create_new<timer>(timer_func, args...);
	timerPtr->start(delay);
	return timerPtr;
}

}
}

#endif /* CORE_SOURCES_PROCESS_PROCESSOR_H_ */
